import { useStorage } from "@/utils/storage";
import { useStatusStore, useUserStore, useRefs } from "@/stores";

let baseHOST = '/app/ycMassage';
let baseURL = '/app/ycMassage/api/';
import * as $helper from '@/utils/helper';
// if ($helper.isProd()) {
//     baseHOST = `${$helper.host()}/app/ycMassage`;
//     baseURL = `${$helper.host()}/app/ycMassage/api/`;
// }
const RequestHeaders = (options: any) => {
    const header: any = {};
    const UserStore = useUserStore();
    if (UserStore.hasLogin) {
        const token = UserStore.getToken();
        if (token) {
            header['Authorization'] = token;
        }
    }
    if (options && options.header) {
        for (let x in options.header) {
            header[x] = options.header[x]
        }
    }
    const storeage = useStorage();
    const appid = $helper.appid();
    if (appid) {
        header['Appid'] = appid;
        const icode = storeage.get('ICODE.' + appid);
        if (icode) {
            header['Icode'] = icode;
        }
        const PUID = storeage.get('PUID.' + appid);
        if (PUID) {
            header['Puid'] = PUID;
        }
        const DEALER_UID = storeage.get('DEALER_UID.' + appid);
        if (DEALER_UID) {
            header['Dealeruid'] = DEALER_UID;
        }
        const CHANNELS_ID = storeage.get('CHANNELS_ID.' + appid);
        if (CHANNELS_ID) {
            header['Channelsid'] = CHANNELS_ID;
        }
    }
    const StatusStore = useStatusStore();
    const { GLOBALSTATUS } = useRefs(StatusStore);
    if (GLOBALSTATUS.value.city.id) {
        switch (GLOBALSTATUS.value.city.level) {
            case 0:
                header['Province'] = GLOBALSTATUS.value.city.id;
                break;
            case 1:
                header['City'] = GLOBALSTATUS.value.city.id;
                break;
            case 2:
                header['Area'] = GLOBALSTATUS.value.city.id;
                break;
        }
    }
    if (GLOBALSTATUS.value.location) {
        header['Longitude'] = GLOBALSTATUS.value.location.longitude;
        header['Latitude'] = GLOBALSTATUS.value.location.latitude;
    }
    return header;
}
export const buildUrl = (baseURL: string, url: string) => {
    // 如果是完整的 http(s) URL，直接返回
    if (/http(s)?:\/\//.test(url)) {
        return url;
    }
    // 确保 baseURL 和 url 之间只有一个斜杠
    return baseURL.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
}
export const $http = {
    baseURL,
    baseHOST,
    get: (url: string, options?: any) => {
        return new Promise((resolve, reject) => {
            uni.request({
                ...options,
                header: RequestHeaders(options),
                url: buildUrl(baseURL, url),
                success: (res: any) => {
                    if (res.statusCode === 200) {
                        resolve(res.data);
                    } else {
                        reject(res);
                    }
                },
                fail: (err: any) => {
                    reject(err);
                },
                complete: () => { }
            })
        })
    },
    post: (url: string, options: any) => {
        return new Promise((resolve, reject) => {
            uni.request({
                ...options,
                header: RequestHeaders(options),
                url: buildUrl(baseURL, url),
                method: 'POST',
                success: (res: any) => {
                    if (res.statusCode === 200) {
                        resolve(res.data);
                    } else {
                        reject(res);
                    }
                },
                fail: (err: any) => {
                    reject(err);
                },
                complete: () => { }
            })
        })
    },
    upload: (url: string, options: UniNamespace.UploadFileOption) => {
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                ...options,
                header: RequestHeaders(options),
                url: buildUrl(baseURL, url),
                success: (res) => {
                    if (res.statusCode === 200) {
                        resolve(JSON.parse(res.data));
                    } else {
                        reject(res);
                    }
                },
                fail: (err) => {
                    reject(err);
                },
            })
        })
    }
}
